home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 2.toast / pc / sample code / interapplication comm / texttotypeintlcoercion / texttotypeintlcoercion.c
Encoding:
Text File  |  2000-06-23  |  4.8 KB  |  110 lines

  1. /*
  2.     File:        TEXTtotypeIntlCoercion.c
  3.  
  4.     Contains:    When the AppleEvent Registry and Inside Mac volume VI were first released,
  5.                 the documented type for error strings and other stuff was typeChar ('TEXT').
  6.                 Well, as we went along designing things like AppleScript, we realized
  7.                 that typeChar was not suitable for a lot of developers and countries.  
  8.                 Multi-byte scripts couldn't use typeChar and maintain all the information
  9.                 they needed to, and other Bad Things were implied by typeChar.
  10.  
  11.                 So, the Winter '92 AE Registry introduced a new type, typeIntlText, defined as
  12.                 (quoting from the AERegistry)...
  13.                 A typeIntlText descriptor record contains identifiers for the language and script 
  14.                 system of text followed by the text itself.
  15.                 Description    The dataHandle field of a typeIntlText descriptor record contains the 
  16.                 following:
  17.                     a 2-byte script code that identifies the script system of the text
  18.                     a 2-byte language code that identifies the language of the text
  19.                     the characters in the text (a series of either 1- or 2-byte characters
  20.                     ; the script system determines whether the characters are 1 or 2 bytes in length)
  21.     
  22.                 That's great and wonderful, but heck, a lot of you (and Me) had already 
  23.                 written code that supplied typeChar for errors and other things that should now be
  24.                 typeIntlText (note for you programmers: Don't worry, code really is coming soon).
  25.  
  26.                 What's a Coder to Do?
  27.                 You can fix your stuff so you supply typeIntlText, but what about all the
  28.                 other stuff out there that's giving you back typeChar when you want typeIntlText????
  29.  
  30.                 A Coercion Routine!
  31.  
  32.                 And here it is.  This coercion routine coerces from typeChar to typeIntlText.
  33.                 Put this in your code, then when someone sends you typeChar when you've asked for
  34.                 typeIntlText, this coercion routine will give you what you asked for seamlessly,
  35.                 you won't even know the coercion happened.
  36.  
  37.     Written by: C.K. Haun    
  38.  
  39.     Copyright:    Copyright © 1992-1999 by Apple Computer, Inc., All Rights Reserved.
  40.  
  41.                 You may incorporate this Apple sample source code into your program(s) without
  42.                 restriction. This Apple sample source code has been provided "AS IS" and the
  43.                 responsibility for its operation is yours. You are not permitted to redistribute
  44.                 this Apple sample source code as "Apple sample source code" after having made
  45.                 changes. If you're going to re-distribute the source, we require that you make
  46.                 it clear in the source that the code was descended from Apple sample source
  47.                 code, but that you've made changes.
  48.  
  49.     Change History (most recent first):
  50.                 7/21/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
  51.                 
  52.  
  53. */
  54.  
  55. /* the coercion.  See after this code chunk for code  for installing it */
  56.  
  57. pascal OSErr CoerceTEXTToIntl(DescType origData, Ptr inPtr, Size theSize, DescType toType, long refCon, AEDesc *result)
  58. {
  59. OSErr myErr = noErr;
  60. Handle newDataHandle;
  61. Ptr scratchPtr;
  62. /* first see if the stuff we're coercing is correct.  This _should_ never
  63. really be necessary, since the AEM wouldn't have dispatched to us if it wasn't
  64. right, but Hey, I'm paranoid. */
  65. if(origData == typeText && toType == typeIntlText){
  66.     /* the data handle for typeIntlText contains 
  67.     2 byte script code
  68.     2 byte language code
  69.     the characters.
  70.     So, we'll do something like this....
  71.     */
  72.     /* Get a handle to convert the text to intltext */
  73.     newDataHandle = NewHandle(theSize + 4); /* TEXT size plus those 2 new shorts */
  74.     if(newDataHandle && MemError() == noErr){
  75.         /* got the handle size I needed */
  76.         scratchPtr = *newDataHandle;
  77.         /* I'm going to default the script and language values to system, since I don't know where */
  78.         /* this text is coming from, so I can't assume application script settings */
  79.         /* This means I will be wrong some times, but since this is a stopgap anyway. */
  80.         /* I'll live with it */
  81.         *((short *)scratchPtr = iuCurrentScript;
  82.         scratchPtr = scratchPtr + 2;  /* two bytes further */
  83.         *((short *)scratchPtr = iuCurrentCurLang;
  84.         /* now move the actual bytes */
  85.         HLock(newDataHandle);
  86.         scratchPtr = scratchPtr + 2;  /* two bytes further */
  87.         BlockMove(inPtr,scratchPtr,theSize);
  88.         
  89.         /* put this in the resulting AEDesc for the coercion */
  90.         myErr = AECreateDesc(typeIntlText,*newDataHandle,GetHandleSize(newDataHandle),result);
  91.         /* we've put the data in the desc, get rid of my intermediate handle now */
  92.         DisposHandle(newDataHandle);
  93.         /* and exit, the only error from this bit will be the AECreateDesc error and we can propigate */
  94.         /* that along */
  95.         }
  96.     
  97. } else {
  98. /* bad data passed */
  99.     myErr = errAECoercionFail;
  100. }
  101. return(myErr);
  102. }
  103.  
  104. /* see if there already is one */
  105. installErr = AEGetCoercionHandler(typeChar, typeIntlText, &oldHandler, &oldRefCon, &typeIsDesc, true);
  106. /* if there already was one, I don't install mine */
  107. if(installErr == noErr ){
  108. installErr = AEInstallCoercionHandler(typeChar, typeIntlText, (ProcPtr)CoerceTEXTToIntl, nil, false, true);
  109.  
  110. }